package com.apes.enquiry.enquiry.repository;

import com.apes.enquiry.enquiry.model.QuoteResult;
import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Set;

/**
 * 类描述：
 *
 * @ClassName QuoteResultRepository
 * @Description 描述
 * @Author wushilong
 * @Date 2023/3/20 16:44
 * @Version 1.0
 */
public interface QuoteResultRepository extends CustomRepository<QuoteResult, String> {

    @Query(value = "select a from QuoteResult a where a.enquiryItem = :enquiryItemId and a.supplierId = :supplierId and a.quoteId = :quoteId and a.state <> '20' and a.latestFlag <> 0 ")
    List<QuoteResult> findAllEnquiryItem(String enquiryItemId, String supplierId, String quoteId);

    @Query(value = "select a from QuoteResult a where a.enquiryId = :enquiryId  and a.state <> '20' and a.notTaxUnitPrice <> 0 and a.latestFlag <> 0")
    List<QuoteResult> findByEnquiryId(String enquiryId);

    @Query(value = "select a from QuoteResult a where a.quoteId = :quoteId and a.enquiryItem = :enquiryItem")
    List<QuoteResult> findOneQuote(String quoteId, String enquiryItem);

    @Query(value = "select a from QuoteResult a where a.enquiryId = :enquiryId and a.id = :id")
    List<QuoteResult> findByEnquiryIdByProduct(String enquiryId, String id);

    @Query(value = "select a from QuoteResult a where a.quoteId = :quoteId")
    List<QuoteResult> findByQuoteId(String quoteId);

    @Query(value = "select a from QuoteResult a where a.priceNo not in ?1 and a.quoteId = ?2")
    List<QuoteResult> findNoInPriceNo(Set<String> priceNo, String quoteId);

    @Query(value = "select a from QuoteResult a where a.priceNo = ?1 and a.quoteId = ?2 and a.latestFlag = 1 and a.quotaValid = 1 ")
    List<QuoteResult> accordPriceNo(String priceNo, String quoteId);

    @Modifying
    @Query(value = "update QuoteResult a set a.latestFlag = 0 where a.quoteId = :quoteId")
    void updateFlagByQuoteId(String quoteId);

    @Query(value = "select a from QuoteResult a where a.enquiryItem = :enquiryItemId and a.supplierId = :supplierId " +
            "and a.quoteId = :quoteId and a.state <> '20' and a.notTaxUnitPrice > 0 and a.latestFlag <> 0 ")
    List<QuoteResult> findAllEnquiryItemAndPriceGTZero(String enquiryItemId, String supplierId, String quoteId);
}
